﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TJADRICD.Model;
using System.Data.SqlClient;

namespace TJADRICD.DLL
{
   public class VersionNumberDll
    {
        private string tableName = "vnnumber";//表名
       // private string projectNumber = "";//项目编号
        //private string subProjectNumber = "";//子项目编号
        //private string sheetTittleChinese = "";//图纸中文名称
        //private string disciplineChinese = "";//专业中文名称
        //private string stageChinese = "";//阶段中文名称
        //private string sheetNo = "";

        //获得版本号信息
        public VersionNumber getVersionNumber(string pn, string spn, string stc, string dc, string sc, string sn)
        {
          //string selectStr = "select * from " + tableName+" where pn='"+pn+"' and spn='"+spn+"' and stc='"+stc+"' and dc='"+dc+"' and sc='"+sc+"' and sn="+sn;
          string selectStr = "select * from " + tableName + " where pn='" + pn + "' and spn='" + spn + "' and stc='" + stc + "' and dc='" + dc + "' and sc='" + sc + "' and sn=" + sn +" order by audit";
           // string selectStr = "select * from " + tableName + " where pn=" + pn + " and spn=" + spn + " and stc=" + stc;
            try
            {
                SqlConnection conn = Connection.getSqlConnection(CONST.connstr);
                SqlCommand cmd = new SqlCommand(selectStr, conn);
                conn.Open();
                SqlDataReader sdr = cmd.ExecuteReader();
                if (sdr.Read())
                {
                    VersionNumber vn = new VersionNumber();
                    vn.ProjectNumber = sdr["pn"].ToString();
                    vn.SubProjectNumber = sdr["spn"].ToString();
                    vn.SheetTittleChinese = sdr["stc"].ToString();
                    vn.DisciplineChinese = sdr["dc"].ToString();
                    vn.StageChinese = sdr["sc"].ToString();
                    vn.SheetNo = sdr["sn"].ToString();
                    vn.VersionNo = (char)sdr["vn"].ToString().ToCharArray()[0];
                    vn.Audit=sdr["audit"].ToString().Equals("1")?true:false;
                    return vn;
                }
                conn.Close();
                sdr.Close();
                return null;
            }
            catch(Exception e)
            {
                Console.Write("版本信息数据库异常："+e.Message);
            }
            return null;
        }
      //更新图纸版本号
        public Boolean updateVersionNumber(char newVersionNumber, string pn, string spn, string stc, string dc, string sc, string sn)
        {
            string updateStr = "update "+tableName+" set vn='"+newVersionNumber+"' where pn='"+pn+"' and spn='"+spn+"' and stc='"+stc+"' and dc='"+dc+"' and sc='"+sc+"' and sn='"+sn+"'";
            try
            {
                SqlConnection conn = Connection.getSqlConnection(CONST.connstr);
                SqlCommand cmd = new SqlCommand(updateStr, conn);
                conn.Open();
                if (cmd.ExecuteNonQuery() > 0)
                {
                    conn.Close();
                    return true;
                }
                conn.Close();
            }
            catch (Exception e)
            {
                Console.Write("更新图纸版本数据发生异常："+e.Message.ToString());
            }
            return false;
        }

       //插入图纸版本信息
        public Boolean insertVersionNumber(char versionNumber,string pn,string spn,string stc,string dc,string sc,string sn)
        {
            string insertStr = "insert into " + tableName + "(pn,spn,stc,dc,sc,sn,vn,audit)  values('"+pn+"','"+spn+"','"+stc+"','"+dc+"','"+sc+"','"+sn+"','"+versionNumber+"','0')";
            try
            {
                SqlConnection conn = Connection.getSqlConnection(CONST.connstr);
                SqlCommand cmd = new SqlCommand(insertStr, conn);
                conn.Open();
                if (cmd.ExecuteNonQuery() > 0)
                {
                    conn.Close();
                    return true;
                }
                conn.Close();
            }
            catch (Exception e)
            {
                Console.Write("插入图纸版本数据发生异常："+e.Message.ToString());
            }
            return false;
        }

    }
}
